package com.fengchao.api.service;

import com.fengchao.api.config.Constant;
import com.fengchao.api.dao.bci.*;
import com.fengchao.api.dao.usr.UsrInfoMapper;
import com.fengchao.api.dao.usr.UsrPersonalMessageMapper;
import com.fengchao.api.dao.usr.UsrSharedMessageOptInMapper;
import com.fengchao.api.dao.usr.UsrUserFocusArtistMapper;
import com.fengchao.model.bci.*;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.usr.UsrInfo;
import com.fengchao.model.usr.UsrUserFocusArtist;
import com.fengchao.util.common.CommonValidator;
import com.fengchao.util.common.DateUtil;
import org.junit.runner.notification.RunListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.fengchao.api.config.Constant.DISABLE;


/**
 * Created by wangnina 6/6.
 */
@Service
public class BciService {

    @Autowired
    private BciArtistInfoMapper bciArtistInfoMapper;
    @Autowired
    private UsrUserFocusArtistMapper usrUserFocusArtistMapper;
    @Autowired
    private UsrInfoMapper usrInfoMapper;
    @Autowired
    private BciOrganizerInfoMapper bciOrganizerInfoMapper;
    @Autowired
    private BciConcertScheduleMapper bciConcertScheduleMapper;
    @Autowired
    private BciConcertScheduleArtistsMapper bciConcertScheduleArtistsMapper;
    @Autowired
    private BciConcertScheduleDateMapper bciConcertScheduleDateMapper;
    @Autowired
    private BciArtistSummaryMapper bciArtistSummaryMapper;
    @Autowired
    private BciFieldInfoMapper bciFieldInfoMapper;
    @Autowired
    private BciFieldImagesMapper bciFieldImagesMapper;


    private static Logger logger = LoggerFactory.getLogger(BciService.class);


    /**
     * 根据字母分页查询艺人列表
     *
     * @param letter
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findArtistByNamePyAndPage(Integer userId, String letter, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<BciArtistInfo> lists = bciArtistInfoMapper.findArtistByNamePyAndIsDeletedAndStatusAndPage(letter, false, Constant.ENABLED, start, count);
        return forBciArtistInfo(lists, userId);
    }

    /**
     * 循环查询出来的艺人信息
     *
     * @param lists
     * @param userId
     * @return
     */
    public List<Map<String, Object>> forBciArtistInfo(List<BciArtistInfo> lists, Integer userId) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (BciArtistInfo bciArtistInfo : lists) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", bciArtistInfo.getId());//app头像
            map.put("headIconApp", bciArtistInfo.getHeadIconApp());//app头像
            map.put("headIconPc", bciArtistInfo.getHeadIconPc());//pc头像
            map.put("nameEn", bciArtistInfo.getNameEn());
            map.put("nameZh", bciArtistInfo.getNameZh());
            map.put("nameDisplay", bciArtistInfo.getNameDisplay());
            map.put("sex", bciArtistInfo.getSex());
            map.put("locationType", Constant.BCI_ARTI_IOCATIONTYPE_MAP.get(bciArtistInfo.getLocationType()) == null ? "" : Constant.BCI_ARTI_IOCATIONTYPE_MAP.get(bciArtistInfo.getLocationType()));//地区风格
            map.put("artistInfo", "");//艺人信息    单独一个接口获取
            map.put("nationality", bciArtistInfo.getNationality());//国籍
            map.put("musicStyle", bciArtistInfo.getMusicStyle());//歌曲风格
            map.put("represent", bciArtistInfo.getRepresent());//歌曲代表作
            //查询艺人的粉丝的数量
            Integer focusCount = usrUserFocusArtistMapper.findCountByArtistId(bciArtistInfo.getId(), null);
            map.put("focusCount", focusCount);
            //查询该用户是否关注了该艺人
            Integer isFocus = 0;
            if (CommonValidator.isStringsNotNull(userId)) {
                isFocus = usrUserFocusArtistMapper.findCountByArtistId(bciArtistInfo.getId(), userId);
            }
            map.put("isFocus", isFocus > 1 ? 1 : isFocus);
            resultList.add(map);
        }
        return resultList;
    }

    /**
     * 获取首页的推荐艺人
     *
     * @param count
     * @return
     */
    public List<Map<String, Object>> getHomeRecommendedArtist(Integer count) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        //查询n条艺人信息
        List<BciArtistInfo> artistInfos = bciArtistInfoMapper.findArtistByRecommended(0, count);
        Integer differ = count - artistInfos.size();//求出相差多少
        if (differ > 0) {
            List<BciArtistInfo> artistInfos1 = bciArtistInfoMapper.findArtistBySigned(0, 200);//取签约的艺人

            if (count > artistInfos1.size()) {//如count大于查询出来的值 直接和并返回
                artistInfos.addAll(artistInfos1);
            } else {//否则随机取
                Map<Integer, Integer> map = new HashMap<>();//排重

                for (int i = 0; i < differ; i++) {
                    int index = 0;
                    while (true) {
                        Random random = new Random();
                        index = random.nextInt(artistInfos1.size());
                        if (map.get(index) == null) {
                            break;
                        }
                    }
                    map.put(index, index);
                    artistInfos.add(artistInfos1.get(index));
                }
            }
        }
        for (BciArtistInfo bciArtistInfo : artistInfos) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("artistId", bciArtistInfo.getId());
            resultMap.put("nameDisplay", bciArtistInfo.getNameDisplay());
            resultMap.put("headPc", bciArtistInfo.getHeadIconPc());
            resultMap.put("headApp", bciArtistInfo.getHeadIconApp());
            resultMap.put("style", bciArtistInfo.getMusicStyle());
            resultList.add(resultMap);
        }
        return resultList;
    }

    /**
     * 查询心动艺人的列表
     *
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> getBciArtistSummaryList(Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<BciArtistSummary> bciArtistSummaryList = bciArtistSummaryMapper.findBciArtistSummaryOrder(start, count);
        return forBciArtistSummaryList(bciArtistSummaryList);
    }

    /**
     * 循环心动艺人信息
     *
     * @param bciArtistSummaryList
     * @return
     */
    public List<Map<String, Object>> forBciArtistSummaryList(List<BciArtistSummary> bciArtistSummaryList) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (BciArtistSummary bciArtistSummary : bciArtistSummaryList) {
            Map<String, Object> resultMap = new HashMap<>();
            String name = "";
            if (bciArtistSummary != null) {
                if (CommonValidator.isStringsNotNull(bciArtistSummary.getArtistNameEn())) {
                    name = bciArtistSummary.getArtistNameEn();
                }
                if (CommonValidator.isStringsNotNull(bciArtistSummary.getArtistNameCn())) {
                    name = bciArtistSummary.getArtistNameCn();
                }
                if (CommonValidator.isStringsNotNull(bciArtistSummary.getArtistNameEn()) && CommonValidator.isStringsNotNull(bciArtistSummary.getArtistNameCn())) {
                    name = bciArtistSummary.getArtistNameCn() + "(" + bciArtistSummary.getArtistNameEn() + ")";
                }
            }
            resultMap.put("name", name);
            resultMap.put("region", bciArtistSummary.getBirthAddr() == null ? "" : bciArtistSummary.getBirthAddr());
            resultMap.put("id", bciArtistSummary.getId() == null ? "" : bciArtistSummary.getId());
            resultMap.put("headIcon", bciArtistSummary.getHeadIconUrl() == null ? "" : bciArtistSummary.getHeadIconUrl());
            resultList.add(resultMap);
        }
        return resultList;
    }

    /**
     * 根据字母模糊查询列表总数
     *
     * @param letter
     * @return
     */
    public Integer findArtistByNamePyAndPageTotal(String letter) {
        return bciArtistInfoMapper.findArtistByNamePyAndIsDeletedAndStatusAndPageTotal(letter, false, Constant.ENABLED);
    }


    /**
     * 根据Id查询艺人
     *
     * @param id
     * @return
     */
    public Map<String, Object> findArtistById(Integer userId, Integer id) {
        BciArtistInfo bciArtistInfo = bciArtistInfoMapper.selectByPrimaryKey(id);
        if (bciArtistInfo != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("headIconApp", bciArtistInfo.getHeadIconApp());
            map.put("headIconPc", bciArtistInfo.getHeadIconPc());
            map.put("detailTitleImageUrl", bciArtistInfo.getDetailTitleImageUrl());
            map.put("nameZh", bciArtistInfo.getNameZh());
            map.put("nameDisplay", bciArtistInfo.getNameDisplay());
            map.put("sex", bciArtistInfo.getSex());
            map.put("nameEn", bciArtistInfo.getNameEn());
            map.put("namePy", bciArtistInfo.getNamePy());
            map.put("artistInfo", "");//艺人信息 单独一个接口获取
            map.put("nationality", bciArtistInfo.getNationality());
            map.put("locationType", Constant.BCI_ARTI_IOCATIONTYPE_MAP.get(bciArtistInfo.getLocationType()) == null ? "" : Constant.BCI_ARTI_IOCATIONTYPE_MAP.get(bciArtistInfo.getLocationType()));//地区风格
            map.put("musicStyle", bciArtistInfo.getMusicStyle());
            map.put("represent", bciArtistInfo.getRepresent());
            //查询艺人的粉丝的数量
            Integer focusCount = usrUserFocusArtistMapper.findCountByArtistId(bciArtistInfo.getId(), null);
            map.put("focusCount", focusCount);
            //查询该用户是否关注了该艺人
            Integer isFocus = usrUserFocusArtistMapper.findCountByArtistId(bciArtistInfo.getId(), userId);
            map.put("isFocus", isFocus > 1 ? 1 : isFocus);
            String artistInfoLink = Constant.CONFIGMAP.get("artistInfoLink");
            map.put("artistInfoLink", artistInfoLink + bciArtistInfo.getId());
            return map;
        }
        return null;
    }

    /**
     * 根据Id查询艺人简介info
     *
     * @return
     */
    public String findArtistInfoByid(Integer id) {
        BciArtistInfo bciArtistInfo = bciArtistInfoMapper.selectByPrimaryKey(id);
        if (bciArtistInfo != null) {
            return bciArtistInfo.getArtistInfo();
        }
        return null;
    }

    /**
     * 查询userId关注的艺人
     *
     * @return
     */
    public List<Map<String, Object>> findArtistByFocusUserId(Integer userId, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Map<String, Object>> list = bciArtistInfoMapper.findArtistByFocusUserId(userId, start, count);
        for (Map<String, Object> map : list) {
            map.put("isFocus", 1);//这里有个傻瓜让我加的 哈哈哈哈 多加个没用的循环
            resultList.add(map);
        }
        return resultList;
    }

    /**
     * 添加艺人
     *
     * @param userId
     * @param name
     * @param nationality
     * @param musicStyle
     * @param sex
     * @param email
     * @return
     */
    @Transactional
    public Boolean authenticationArtist(Integer userId, String name, String mobile, String nationality, String
            musicStyle, String sex, String email, String artstInfo, String artistMemo, String headIconPc) {
        BciArtistInfo bciArtistInfo = bciArtistInfoMapper.findArtistByUserId(userId);
        if (bciArtistInfo == null) {
            bciArtistInfo = new BciArtistInfo();
        }
        bciArtistInfo.setStatus(DISABLE);//未启用
        bciArtistInfo.setNameDisplay(name);
        bciArtistInfo.setMobile(mobile);
        bciArtistInfo.setNationality(nationality);
        bciArtistInfo.setMusicStyle(musicStyle);
        bciArtistInfo.setSex(sex);
        bciArtistInfo.setEmail(email);
        bciArtistInfo.setUserId(userId);
        bciArtistInfo.setHeadIconPc(headIconPc);
        bciArtistInfo.setArtistInfo(artstInfo);
        bciArtistInfo.setArtistMemo(artistMemo);
        bciArtistInfo.setCreateTime(new Date());
        if (bciArtistInfo.getId() == null) {
            Integer count = bciArtistInfoMapper.insertSelective(bciArtistInfo);
        } else {
            Integer count = bciArtistInfoMapper.updateByPrimaryKeySelective(bciArtistInfo);
        }
        return true;
    }

    /**
     * 获取该用户认证艺人的资料
     *
     * @param userId
     * @return
     */
    public BciArtistInfo getAuthenticationArtist(Integer userId) {
        BciArtistInfo bciArtistInfo = bciArtistInfoMapper.findArtistByUserId(userId);
        return bciArtistInfo;
    }

    /**
     * 获取该用户的认证主办方的资料
     *
     * @param userId
     * @return
     */
    public BciOrganizerInfo getAuthenticationOrganizer(Integer userId) {
        BciOrganizerInfo bciOrganizerInfo = bciOrganizerInfoMapper.findOrganizerByUserId(userId);
        return bciOrganizerInfo;
    }

    /**
     * 认证主办方
     *
     * @param userId
     * @param organizerName
     * @param organizerType
     * @param identity
     * @param licenseNumber
     * @param identityFront
     * @param identtityReverses
     * @param licenseScanImageUrl
     * @param authenticationFileUrl
     * @param contactMobile
     * @param contactEmail
     * @param companyInfo
     * @param memo
     * @return
     */
    @Transactional
    public Boolean authenticationOrganizer(Integer userId, String organizerName, String organizerHead, String
            organizerType,
                                           String identity, String licenseNumber, String identityFront,
                                           String identtityReverses, String licenseScanImageUrl, String authenticationFileUrl,
                                           String contactMobile, String contactEmail, String companyInfo, String memo) {
        BciOrganizerInfo bciOrganizerInfo = bciOrganizerInfoMapper.findOrganizerByUserId(userId);
        if (bciOrganizerInfo == null) {
            bciOrganizerInfo = new BciOrganizerInfo();
        }
        bciOrganizerInfo.setUserId(userId);
        bciOrganizerInfo.setOrganizerName(organizerName);
        bciOrganizerInfo.setOrganizerHead(organizerHead);
        bciOrganizerInfo.setOrganizerType(organizerType);
        bciOrganizerInfo.setContactIdentity(identity);
        bciOrganizerInfo.setContactIdentityFront(identityFront);
        bciOrganizerInfo.setContactIdentityReverses(identtityReverses);
        bciOrganizerInfo.setLicenseNumber(licenseNumber);
        bciOrganizerInfo.setLicenseScanImageUrl(licenseScanImageUrl);
        bciOrganizerInfo.setAuthenticationFileUrl(authenticationFileUrl);
        bciOrganizerInfo.setContactMobile(contactMobile);
        bciOrganizerInfo.setContactEmail(contactEmail);
        bciOrganizerInfo.setCompanyInfo(companyInfo);
        bciOrganizerInfo.setMemo(memo);
        bciOrganizerInfo.setStatus(DISABLE);
        bciOrganizerInfo.setCreateTime(new Date());
        if (bciOrganizerInfo.getId() == null) {
            Integer count = bciOrganizerInfoMapper.insertSelective(bciOrganizerInfo);
        } else {
            Integer count = bciOrganizerInfoMapper.updateByPrimaryKeySelective(bciOrganizerInfo);
        }
        return true;
    }

    /**
     * 获取用户的身份状态
     *
     * @param userId
     * @return
     */
    public Map<Object, Object> getIdentityStatus(Integer userId) {
        Map<Object, Object> resultMap = new HashMap<>();
        //判断是不是艺人
        BciArtistInfo bciArtistInfo = bciArtistInfoMapper.findArtistByUserId(userId);
        //判断是不是主办方
        BciOrganizerInfo bciOrganizerInfo = bciOrganizerInfoMapper.findOrganizerByUserId(userId);
        //判断是不是场地
        resultMap.put("identityStatus", Constant.FANS);
        resultMap.put("status", Constant.ENABLED);
        BciFieldInfo bciFieldInfo = bciFieldInfoMapper.findBciFieldInfoByUserId(userId);
        if (bciArtistInfo != null) {
            resultMap.put("identityStatus", Constant.ARTIST);
            resultMap.put("status", bciArtistInfo.getStatus());
        }
        if (bciOrganizerInfo != null) {
            if (bciArtistInfo != null) {
                if (bciOrganizerInfo.getCreateTime().compareTo(bciArtistInfo.getCreateTime()) > 0) {
                    resultMap.put("identityStatus", Constant.ORGANIZER);
                    resultMap.put("status", bciOrganizerInfo.getStatus());
                }
            } else {
                resultMap.put("identityStatus", Constant.ORGANIZER);
                resultMap.put("status", bciOrganizerInfo.getStatus());
            }
        }
        if (bciFieldInfo != null) {
            if (bciArtistInfo != null && bciOrganizerInfo != null) {
                if (bciFieldInfo.getCreateTime().compareTo(bciArtistInfo.getCreateTime()) > 0 && bciFieldInfo.getCreateTime().compareTo(bciOrganizerInfo.getCreateTime()) > 0) {
                    resultMap.put("identityStatus", Constant.FIELD_PROVIDER);
                    resultMap.put("status", bciFieldInfo.getStatus());
                }
            } else if (bciArtistInfo != null) {
                if (bciFieldInfo.getCreateTime().compareTo(bciArtistInfo.getCreateTime()) > 0) {
                    resultMap.put("identityStatus", Constant.FIELD_PROVIDER);
                    resultMap.put("status", bciFieldInfo.getStatus());
                }
            } else if (bciOrganizerInfo != null) {
                if (bciFieldInfo.getCreateTime().compareTo(bciOrganizerInfo.getCreateTime()) > 0) {
                    resultMap.put("identityStatus", Constant.FIELD_PROVIDER);
                    resultMap.put("status", bciFieldInfo.getStatus());
                }
            } else {
                resultMap.put("identityStatus", Constant.FIELD_PROVIDER);
                resultMap.put("status", bciFieldInfo.getStatus());
            }
        }
        return resultMap;
    }

    /**
     * 编辑行程
     *
     * @param isShared
     * @param startTime
     * @param days
     * @return
     */
    @Transactional
    public void editSchedule(Integer scheduleId, Integer userId, Integer isShared, Date startTime, Integer
            days, String city, String fieldName, String artistName, String concertInfo) {
        BciConcertSchedule bciConcertSchedule = new BciConcertSchedule();
        bciConcertSchedule.setUserId(userId);
        bciConcertSchedule.setStartTime(startTime);
        bciConcertSchedule.setDays(days);
        if (days > 1) {
            bciConcertSchedule.setEndTime(DateUtil.addDay(startTime, days - 1));
        } else {
            bciConcertSchedule.setEndTime(startTime);
        }
        bciConcertSchedule.setCity(city);
        bciConcertSchedule.setFieldName(fieldName);
        bciConcertSchedule.setConcertInfo(concertInfo);
        bciConcertSchedule.setIsShared(isShared == 1 ? true : false);
        bciConcertSchedule.setIsDeleted(false);
        bciConcertSchedule.setCreateTime(new Date());
        if (scheduleId == null) {//如果是添加
            bciConcertScheduleMapper.insertSelective(bciConcertSchedule);
        } else {//修改
            BciConcertSchedule bciConcertSchedule1 = bciConcertScheduleMapper.selectByPrimaryKey(scheduleId);
            if (bciConcertSchedule1 == null || !userId.equals(bciConcertSchedule.getUserId())) {
                logger.warn("查询的日程id不存在或者创建的userId不对 scheduleId:{},userId:{}", scheduleId, userId);
                throw new CustomBizException("操作日程有误");
            }
            bciConcertSchedule.setId(scheduleId);
            bciConcertScheduleMapper.updateByPrimaryKeySelective(bciConcertSchedule);
            //修改先删除原先的日程信息
            bciConcertScheduleArtistsMapper.delectBciConcertScheduleArtistsByScheduleId(bciConcertSchedule.getId());
            bciConcertScheduleDateMapper.delectScheduleDateByScheduleId(bciConcertSchedule.getId());
        }
        //重新设置日程日期
        for (int i = 0; i < bciConcertSchedule.getDays(); i++) {
            BciConcertScheduleDate bciConcertScheduleDate = new BciConcertScheduleDate();
            bciConcertScheduleDate.setScheduleId(bciConcertSchedule.getId());
            bciConcertScheduleDate.setScheduleDate(DateUtil.addDay(bciConcertSchedule.getStartTime(), i));
            bciConcertScheduleDateMapper.insertSelective(bciConcertScheduleDate);
        }
        //重新设置日程艺人的关联表
        BciConcertScheduleArtists bciConcertScheduleArtists = new BciConcertScheduleArtists();
        bciConcertScheduleArtists.setArtistName(artistName);
        bciConcertScheduleArtists.setScheduleId(bciConcertSchedule.getId());
        bciConcertScheduleArtistsMapper.insertSelective(bciConcertScheduleArtists);
    }

    /**
     * 获取一月的行程时间
     *
     * @return
     */
    public List<Map<String, Object>> getDateSchedule(String monthTime, String monthTime1, String artistName, String
            fieldName) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<BciConcertSchedule> bciConcertScheduleList = bciConcertScheduleMapper.findBciConcertScheduleByMonthTimeAndMonthTime1AndArtistNameFieldName(monthTime, monthTime1, artistName, fieldName);
        for (BciConcertSchedule bciConcertSchedule : bciConcertScheduleList) {
            List<BciConcertScheduleDate> bciConcertScheduleDateList = bciConcertScheduleDateMapper.findBciConcertScheduleDateByScheduleId(bciConcertSchedule.getId());
            for (BciConcertScheduleDate bciConcertScheduleDate : bciConcertScheduleDateList) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("time", bciConcertScheduleDate.getScheduleDate());
                resultList.add(resultMap);
            }
        }
        return resultList;
    }

    /**
     * 根据时间艺人名字场地名获取行程
     *
     * @param daysTime
     * @param artistName
     * @param fieldName
     * @return
     */
    public List<Map<String, Object>> getDaysSchedule(String daysTime, String artistName, String fieldName) {
        return bciConcertScheduleMapper.findScheduleByDaysTime(daysTime, artistName, fieldName);
    }

    /**
     * 根据id查询行程
     *
     * @param scheduleId
     * @return
     */
    public Map<String, Object> getScheduleByid(Integer scheduleId) {
        Map<String, Object> resultMap = new HashMap<>();
        BciConcertSchedule bciConcertSchedule = bciConcertScheduleMapper.selectByPrimaryKey(scheduleId);
        if (bciConcertSchedule == null) {
            logger.warn("查询的日程id不存在 scheduleId:{}", scheduleId);
            throw new CustomBizException("操作日程有误");
        }
        resultMap.put("scheduleId", scheduleId);
        resultMap.put("isShared", bciConcertSchedule.getIsShared() ? 1 : 0);
        resultMap.put("startTime", bciConcertSchedule.getStartTime());
        resultMap.put("days", bciConcertSchedule.getDays());
        resultMap.put("city", bciConcertSchedule.getCity());
        resultMap.put("fieldName", bciConcertSchedule.getFieldName());
        resultMap.put("concertInfo", bciConcertSchedule.getConcertInfo());
        List<BciConcertScheduleArtists> bciConcertScheduleArtistsList = bciConcertScheduleArtistsMapper.findBciConcertScheduleArtistsByScheduleId(scheduleId);
        if (bciConcertScheduleArtistsList.size() > 0) {
            resultMap.put("artistName", bciConcertScheduleArtistsList.get(0).getArtistName());
        } else {
            resultMap.put("artistName", "");
        }
        return resultMap;
    }

    /**
     * 删除行程
     *
     * @param scheduleId
     * @return
     */
    @Transactional
    public void deleteScheduleId(Integer userId, Integer scheduleId) {
        BciConcertSchedule bciConcertSchedule = bciConcertScheduleMapper.selectByPrimaryKey(scheduleId);
        if (bciConcertSchedule == null || !userId.equals(bciConcertSchedule.getUserId())) {//如果等于空，或者userId不等于这个日程
            logger.warn("查询的日程id不存在或者创建的userId不对 scheduleId:{},userId:{}", scheduleId, userId);
            throw new CustomBizException("操作日程有误");
        }
        bciConcertSchedule.setIsDeleted(true);
        bciConcertScheduleMapper.updateByPrimaryKeySelective(bciConcertSchedule);
        bciConcertScheduleArtistsMapper.delectBciConcertScheduleArtistsByScheduleId(scheduleId);
        bciConcertScheduleDateMapper.delectScheduleDateByScheduleId(scheduleId);
    }

    /**
     * 获取UserId的行程(按时间)
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> getMeSchedule(Integer userId, String startTime, String endTime, Integer
            page, Integer count) {
        Integer start = (page - 1) * count;
        return bciConcertScheduleMapper.findMeSchedule(userId, startTime, endTime, start, count);
    }


    /**
     * 获取UserId的行程(按时间) 返回总条数
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    public Integer getMeScheduleTotal(Integer userId, String startTime, String endTime) {
        return bciConcertScheduleMapper.findMeScheduleTotal(userId, startTime, endTime);
    }

    /**
     * 获取艺人的粉丝列表
     *
     * @return
     */
    public Map<Object, Object> getFocusList(Integer userId, Integer page, Integer count) {
        Map<Object, Object> map = new HashMap<>();
        BciArtistInfo bciArtistInfo = bciArtistInfoMapper.findArtistByUserId(userId);
        if (bciArtistInfo == null) {
            logger.warn("该用户不是艺人");
            throw new CustomBizException("该用户不是艺人");
        }
        Integer start = (page - 1) * count;
        List<UsrUserFocusArtist> bciArtistInfos = usrUserFocusArtistMapper.findUserIdByArtistId(bciArtistInfo.getId(), start, count);

        List<Map<Object, Object>> resultList = new ArrayList<>();
        for (UsrUserFocusArtist usrUserFocusArtist : bciArtistInfos) {
            Map<Object, Object> resultMap = new HashMap<>();
            UsrInfo usrInfo = usrInfoMapper.selectByPrimaryKey(usrUserFocusArtist.getUserId());
            if (usrInfo == null) {
                logger.warn("没有找到该粉丝，userId:{}", usrUserFocusArtist.getUserId());
                continue;
            }
            resultMap.put("userId", usrInfo.getId());
            resultMap.put("userName", usrInfo.getNicknameToEmoji() == null ? "匿名" : usrInfo.getNicknameToEmoji());
            resultMap.put("userIcon", usrInfo.getHeadIcon() == null ? "" : usrInfo.getHeadIcon());
            resultMap.put("focusTime", usrUserFocusArtist.getCreateTime());
            resultMap.put("text", "关注了您");
            resultList.add(resultMap);
        }
        //更新为最新的查看点赞的时间
        BciArtistInfo bciArtistInfo1 = new BciArtistInfo();
        bciArtistInfo1.setId(bciArtistInfo.getId());
        bciArtistInfo1.setLastFocusTime(new Date());
        bciArtistInfoMapper.updateByPrimaryKeySelective(bciArtistInfo1);

        Integer focusCount = usrUserFocusArtistMapper.findCountByArtistId(bciArtistInfo.getId(), null);

        map.put("info", resultList);
        map.put("total", focusCount);
        return map;
    }

    /**
     * 添加场地
     *
     * @param fieldName
     * @param fieldType
     * @param fieldAddress
     * @param contactName
     * @param contactMobile
     * @param contactEmail
     * @param licenseNumber
     * @param licenseScanImageUrl
     * @param imageUrl
     * @param fieldInfo
     * @param memo
     */
    @Transactional
    public void authenticationField(Integer userId, String fieldName, String fieldType, String fieldHead, String fieldAddress, String contactName, String contactMobile,
                                    String contactEmail, String licenseNumber, String licenseScanImageUrl, String imageUrl, String fieldInfo,
                                    String memo) {
        BciFieldInfo bciFieldInfo = bciFieldInfoMapper.findBciFieldInfoByUserId(userId);
        if (bciFieldInfo == null) {
            bciFieldInfo = new BciFieldInfo();
        }
        bciFieldInfo.setUserId(userId);
        bciFieldInfo.setLicenseNumber(licenseNumber);
        bciFieldInfo.setLicenseScanImageUrl(licenseScanImageUrl);
        bciFieldInfo.setFieldLogo(fieldHead);
        bciFieldInfo.setFieldName(fieldName);
        bciFieldInfo.setFieldType(fieldType);
        bciFieldInfo.setFieldAddress(fieldAddress);
        bciFieldInfo.setFieldInfo(fieldInfo);
        bciFieldInfo.setContactName(contactName);
        bciFieldInfo.setContactMobile(contactMobile);
        bciFieldInfo.setContactEmail(contactEmail);
        bciFieldInfo.setMemo(memo);
        bciFieldInfo.setStatus(DISABLE);
        bciFieldInfo.setCreateTime(new Date());
        if (bciFieldInfo.getId() == null) {
            bciFieldInfoMapper.insertSelective(bciFieldInfo);
        } else {
            bciFieldInfoMapper.updateByPrimaryKeySelective(bciFieldInfo);
            bciFieldImagesMapper.deleteBciFieldImagesByFieldId(bciFieldInfo.getId());
        }
        BciFieldImages bciFieldImages = new BciFieldImages();
        bciFieldImages.setFieldId(bciFieldInfo.getId());
        bciFieldImages.setImageUrl(imageUrl);
        bciFieldImagesMapper.insertSelective(bciFieldImages);
    }

    /**
     * 获取场地信息
     *
     * @param userId
     * @return
     */
    public BciFieldInfo getField(Integer userId) {
        BciFieldInfo bciFieldInfo = bciFieldInfoMapper.findBciFieldInfoByUserId(userId);
        if (bciFieldInfo != null) {
            bciFieldInfo.setImage(bciFieldImagesMapper.findBciFieldImagesByFieldId(bciFieldInfo.getId()));
        }
        String address = bciFieldInfo.getContactAddress();
        if (address != null) {
            String address1 = address.substring(address.lastIndexOf(" "));
            bciFieldInfo.setContactAddress(address1);
        }
        return bciFieldInfo;
    }
}
